package main.java.com.lee.io;

import java.io.*;

/**
 * created by winston on 2023/11/24
 */
public class ObjectInOutPutStreamTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        var user = new User("winston", 18);
        testWriteObject(user);
        testReadObject();
    }

    public static void testWriteObject(Object object) throws IOException {
        var fileOutputStream = new FileOutputStream("/Users/leejiliang/ideaproject/gitee/demo-multi-thread/user");
        var objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(object);
    }

    public static void testReadObject() throws IOException, ClassNotFoundException {
        var fileInputStream = new FileInputStream("/Users/leejiliang/ideaproject/gitee/demo-multi-thread/user");
        var objectInputStream = new ObjectInputStream(fileInputStream);
        var object = (User) objectInputStream.readObject();
        System.out.println(object);
    }
}

class User implements Serializable {

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
    }
}
